package com.zhulin.android.edoctor.db;

import java.sql.SQLException;

import com.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper;
import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.support.ConnectionSource;
import com.j256.ormlite.table.TableUtils;
import com.zhulin.android.edoctor.jsonobject.JOCaseInfoModel;
import com.zhulin.android.edoctor.jsonobject.JOHospitalModel;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;

public class DBDoctorDBHelp extends OrmLiteSqliteOpenHelper{
	
	public final static String DB_NAME_EDoctor="edoctors.db";
	public final static String DB_Name_Hospital="ehospital.db";
	private static final int DBVERSION = 5;
	
	private Dao<JOCaseInfoModel, Void> daoJoCaseInfoModel;
	private Dao<JOHospitalModel, Void> daoJoHospitalMOdel;
	
	public DBDoctorDBHelp(Context context) {
		super(context, DB_NAME_EDoctor, null, DBVERSION);
	}
	
	public DBDoctorDBHelp(Context context,String DBName){
		super(context, DBName, null, DBVERSION);
	}
	
	@Override
	public void onCreate(SQLiteDatabase sqliteDatabase, ConnectionSource connectionSource) {
		try {
			TableUtils.createTable(connectionSource, JOCaseInfoModel.class);
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	@Override
	public void onUpgrade(SQLiteDatabase sqliteDatabase, ConnectionSource connectionSource, int arg2,
			int arg3) {
		try {
			TableUtils.dropTable(connectionSource, JOCaseInfoModel.class, true);
			onCreate(sqliteDatabase, connectionSource);
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
	public Dao<JOCaseInfoModel, Void> getDaoMyselftCase() throws SQLException {
		if (daoJoCaseInfoModel == null) {
			daoJoCaseInfoModel = getDao(JOCaseInfoModel.class);
		}

		return daoJoCaseInfoModel;
	}
	
	public Dao<JOHospitalModel, Void> getDaoHospital() throws SQLException{
		if(daoJoHospitalMOdel==null){
			daoJoHospitalMOdel=getDao(JOHospitalModel.class);
		}
		
		return daoJoHospitalMOdel;
	}
}